package com.weavernorth.parentalLeave;

import com.weavernorth.util.Logger4jUtil;
import jxl.read.biff.Record;
import org.apache.log4j.Logger;
import weaver.conn.ConnectionPool;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.upgradetool.wscheck.Util;

import java.sql.Connection;
import java.sql.Statement;

import static com.weavernorth.util.dateUtils.*;

/**
 * @Author: bruseewang
 * @Package: com.weavernorth.parentalLeave
 * @date: 2022/1/13 14:12
 * @description: 计划任务-根据建模表中uf_yejxxb数据，释放育儿假
 */
public class ZparentalLeaveTimmer extends BaseCronJob {
    private String gh;
    private String date;

    public String getGh() {
        return gh;
    }

    public void setGh(String gh) {
        this.gh = gh;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
    public static ConnectionPool pool = ConnectionPool.getInstance();
    public Connection conn;
    public Statement statement;
    @Override
    public void execute() {
        Logger log = Logger4jUtil.getLoggerByName("/Timmer/ZparentalLeaveTimmer/");
        RecordSet rs=new RecordSet();
        //获取当前日期
        String nowDate = getYearMonthDay();
        log.info("当前日期："+nowDate);
        if (date != null) {
            if (isValidDate(date, "yyyy-MM-dd")) {
                nowDate = date;
                log.info("日期参数-开始日期：" + nowDate);
            }
        }
        String year = nowDate.split("-")[0];
        String monthDay = nowDate.split("-")[1]+ nowDate.split("-")[2];
        //1.获取育儿假建模表中的所有数据
        String str_query="select * from uf_yejxxb where 1=1";
        if(gh!=null){
            str_query+=" ygbh='"+gh+"'";
        }
        log.info("查询育儿假信息表的sql："+str_query);
        rs.execute(str_query);
        String str_userid="";
        String str_gh="";
        String str_csrq="";
        String csYear="";
        String birthday="";
        String str_jsrq="";
        String sql_opt="";
        try {
        conn = pool.getConnection();
        statement = conn.createStatement();
        while (rs.next()){
            //人员id
            str_userid = Util.null2String(rs.getString("ygxm"));
            //人员编号
            str_gh = Util.null2String(rs.getString("ygbh"));
            //子女出生日期
            str_csrq = Util.null2String(rs.getString("zncsrq"));
            //育儿假到期日期
            str_jsrq = Util.null2String(rs.getString("yejjsrq"));
            //出生年份
            csYear=str_csrq.split("-")[0];
            //生日
            birthday=str_csrq.split("-")[1]+str_csrq.split("-")[2];
            //当前年份大于出生年份
                if(dateBetween(nowDate,str_csrq,str_jsrq)){
                    //如果当前日期为子女生日，则释放育儿假
                    if(birthday.equals(monthDay)){
                        if(isExists(year,str_userid)){
                            //更新现有育儿假
                            sql_opt="UPDATE kq_BalanceOfLeave SET baseAmount=5,usedAmount=0 WHERE RESOURCEID='"+str_userid+"' AND BELONGYEAR='"+year+"' AND leaveRulesId=18";
                            log.info("更新语句：" + sql_opt);
                            statement.addBatch(sql_opt);
                        }else{
                            //插入育儿假
                            sql_opt="insert into kq_BalanceOfLeave (leaveRulesId,resourceId,belongYear,baseAmount,usedAmount) values (18,'"+str_userid+"','"+year+"',5,0) ";
                            log.info("插入语句：" + sql_opt);
                            statement.addBatch(sql_opt);
                        }
                        Long begin1 = System.currentTimeMillis();
                        statement.executeBatch();
                        Long end1 = System.currentTimeMillis();
                        log.info("批量更新/插入耗时：" + (end1 - begin1) + "(毫秒)");
                        conn.close();
                        statement.close();
                    }

                }
        }
        } catch (Exception ex) {
            log.info("程序异常，异常信息："+ex.toString());
        }


    }
    public static boolean isExists(String year,String userid){
        String sql=" SELECT * FROM kq_BalanceOfLeave a,kq_leaveRules b WHERE a.leaveRulesId=b.id AND b.leavename='育儿假'  AND a.resourceId = '"+userid+"' AND a.belongYear='"+year+"'";
        RecordSet rs_check=new RecordSet();
        rs_check.execute(sql);
        if(rs_check.getCounts()>0){
            return true;
        }else{
            return false;
        }
    }
}
